import Vue from 'vue';

export default {
  namespaced: true,
  state: {
    // 通用
    userToken: '',
    userId: '',
    loginType: '',
    realName: '',

    // 猎头 企业管理员
    companyId: '',
    recruiterId: '',

    thumb: '',
  },
  getters: {
    getUserInfo(state) {
      return {
        loginType: parseInt(state.loginType, 10) || parseInt(sessionStorage.getItem('loginType'), 10),
        realName: state.realName || sessionStorage.getItem('realName'),
        userId: state.userId || sessionStorage.getItem('userId'),
        userToken: state.userToken || sessionStorage.getItem('userToken'),
        companyId: state.companyId || sessionStorage.getItem('companyId'),
        recruiterId: state.recruiterId || sessionStorage.getItem('recruiterId'),
        thumb: state.thumb || sessionStorage.getItem('thumb'),
      };
    },
  },
  actions: {
    setUserInfo({ commit }, payload) {
      commit('SET_USER_INFO', { ...payload });
    },
    setThumb({ commit }, payload) {
      commit('SET_USER_THUMB', payload);
    },
    clearUserInfo({ commit }) {
      commit('CLEAR_USER_INFO');
    },
  },
  mutations: {
    SET_USER_INFO(state, { loginType, realName, userId, userToken, companyId, recruiterId }) {
      Vue.set(state, 'loginType', loginType);
      Vue.set(state, 'realName', realName);
      Vue.set(state, 'userId', userId);
      Vue.set(state, 'userToken', userToken);
      Vue.set(state, 'companyId', companyId);
      Vue.set(state, 'recruiterId', recruiterId);
    },
    CLEAR_USER_INFO(state) {
      Vue.set(state, 'loginType', null);
      Vue.set(state, 'realName', null);
      Vue.set(state, 'userId', null);
      Vue.set(state, 'userToken', null);
      Vue.set(state, 'companyId', null);
      Vue.set(state, 'recruiterId', null);
      Vue.set(state, 'thumb', null);
    },
    SET_USER_THUMB(state, thumb) {
      Vue.set(state, 'thumb', thumb);
    },
  },
};
